﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

using System.IO;

namespace HF.Software.ClassLib
{

    public class CardClass
    {
        ///初始化卡
        //参数: acom COM端口;端口从0开始,例如:COM3,参数为2
        //abaud 读卡器的工作频率(9600 --- 115200),机器不同,设置也不同
        [DllImport("icCard.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern bool InitCard(IntPtr aCom, IntPtr aBaud);
        //关闭卡
        [DllImport("icCard.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern bool CloseCard();
        //获取流水号
        [DllImport("icCard.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr ReadCardNo();
        //发声
        //参数为 10 毫秒的倍数
        [DllImport("icCard.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern bool CardShowBeep(IntPtr aTime);

        [DllImport("icCard.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr ReadCardData();

        public static bool netInitCard(int aCom, int aBaud)
        {
            IntPtr com = new IntPtr();
            com = (IntPtr)aCom;
            IntPtr baud = new IntPtr();
            baud = (IntPtr)aBaud;

            return InitCard(com, baud);
        }

        public static bool netCloseCard()
        {
            return CloseCard();
        }

        public static long netReadCardNo()
        {
            IntPtr uip = new IntPtr();
            uip = (IntPtr)0;
            uip = ReadCardNo();
            if ((long)uip != -1)
            {
                CardShowBeep((IntPtr)10);
            }
            return (long)uip;
        }

        public static string fnReadCardData()
        {
            try
            {
                string sIntPtrCardData = ReadCardData().ToString();
                //fnWriteLog(sIntPtrCardData+"---");
                if (!string.IsNullOrWhiteSpace(sIntPtrCardData))                
                {
                    CardShowBeep((IntPtr)10);
                }
                return sIntPtrCardData;
            }
            catch(Exception ex) 
            {
                //fnWriteLog(ex.ToString());
                return "";
            }
        }

        /// <summary>
        /// 写异常日志文件
        /// </summary>
        /// <param name="_sError"></param>
        static void fnWriteLog(string _sError)
        {
            string ErrPath = AppDomain.CurrentDomain.BaseDirectory + "log\\";

            if (!Directory.Exists(ErrPath))
            {
                Directory.CreateDirectory(ErrPath);
            }
            using (StreamWriter sw = new StreamWriter(ErrPath + DateTime.Now.ToString("yyyyMMdd") + @"ErrLog.txt", true))
            {
                sw.WriteLine(_sError);
                sw.WriteLine("---------------------------------------------------------");
                sw.Close();
            }
        }
    }
}
